﻿using AutoMapper;
using MediatR;
using ProduceBasicData.Api.Application.Command.Workstation;
using ProduceBasicData.Domain.FactoryModeling;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.WorkStation
{
    /// <summary>
    /// 工位信息删除处理类
    /// </summary>
    public class DeleteWorkstationHandler : IRequestHandler<DeletetWorkstationCommand, ApiResult<int>>
    {
        private readonly IBaseRespository<WorkstationModel> stationRepo;
        private readonly IMapper mapper;

        public DeleteWorkstationHandler(IBaseRespository<WorkstationModel> stationRepo,
            IMapper mapper)
        {
            this.stationRepo = stationRepo;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(DeletetWorkstationCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            var station=await stationRepo.GetModelAsync(request.Id);
            if (station == null)
            {
                res.Code = ApiEnum.Failed;
                res.Msg = "您要删除的信息不存在";
                return res;
            }
            station.Isdel = true;
            var r=await stationRepo.Update(station);
            if (r>0)
            {
                res.Data = r;
                res.Code = ApiEnum.Success;
                res.Msg = "工位信息删除成功";
            }
            else
            {
                res.Code = ApiEnum.Failed;
                res.Msg = "工位信息删除失败";
            }
            return res;
        }
    }
}
